package cn.org.seventh.web.tag;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.taglibs.standard.lang.support.ExpressionEvaluatorManager;
import org.springframework.context.support.ResourceBundleMessageSource;

import cn.org.seventh.common.StringUtil;

/**
 * @jsp.tag name="out"
 * @author <a href="mailto:ji.li@symbio-group.com">Andy</a>
 * @since Dec 3, 2007 10:55:08 AM
 */
public class OutTag extends BaseTag {

	private static final long serialVersionUID = -536778470887734920L;

	private final transient Log log = LogFactory.getLog(OutTag.class);

	private static final String DEFAULT_OMIT = "...";

	private String value;

	private String omit = DEFAULT_OMIT;

	private String omitKey;

	private boolean iso;

	private int maxWidth;

	private int fixedWidth;

	/**
	 * @jsp.attribute required = "true" rtexprvalue="true"
	 */
	public void setValue(String value) {
		try {
			this.value = (String) ExpressionEvaluatorManager.evaluate("value",
					value, String.class, this, pageContext);
		} catch (JspException e) {
			log.debug(e);
		}
	}

	/**
	 * @jsp.attribute required = "false"
	 * @param omit
	 *            The omit to set.
	 */
	public void setOmit(String omit) {
		this.omit = omit;
	}

	/**
	 * @jsp.attribute required = "false"
	 * @param omitKey
	 *            The omitKey to set.
	 */
	public void setOmitKey(String omitKey) {
		this.omitKey = omitKey;
	}

	/**
	 * @jsp.attribute required = "false"
	 */
	public void setFixedWidth(int fixedWidth) {
		this.fixedWidth = fixedWidth;
	}

	/**
	 * @jsp.attribute required = "false"
	 */
	public void setIso(boolean iso) {
		this.iso = iso;
	}

	/**
	 * @jsp.attribute required = "false"
	 */
	public void setMaxWidth(int maxWidth) {
		this.maxWidth = maxWidth;
	}

	public int doEndTag() throws JspException {
		JspWriter out = pageContext.getOut();
		if (value != null) {
			HttpServletRequest request = (HttpServletRequest) pageContext
					.getRequest();
			if (iso) {
				try {
					value = new String(value.getBytes("ISO-8859-1"), request
							.getCharacterEncoding());
				} catch (Throwable t) {
				}
			}
			try {
				if (fixedWidth > 0) {
					ResourceBundleMessageSource messageSource = getMessageSource();
					if (omitKey != null) {
						omit = messageSource.getMessage(omitKey, null,
								DEFAULT_OMIT, request.getLocale());
					}
					out.print(StringUtil.toSize(value, fixedWidth, omit));
				} else if (maxWidth > 0) {
					ResourceBundleMessageSource messageSource = getMessageSource();
					if (omitKey != null) {
						omit = messageSource.getMessage(omitKey, null,
								DEFAULT_OMIT, request.getLocale());
					}
					out.print(StringUtil.limitString(value, maxWidth, omit));
				} else {
					out.print(value);
				}
			} catch (Exception e) {
				throw new JspException(e);
			}
		}
		return super.doEndTag();
	}
}
